package cn.bs.controller;

import ch.qos.logback.core.util.StringUtil;
import cn.bs.domain.dao.User;
import cn.bs.domain.dto.RegisterDTO;
import cn.bs.domain.dto.LoginDTO;
import cn.bs.domain.dto.UserDTO;
import cn.bs.domain.pojo.Result;
import cn.bs.domain.vo.LoginVO;
import cn.bs.domain.vo.UserVO;
import cn.bs.mapper.UserMapper;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping(value = "/login")
    public Result login(@RequestBody LoginDTO loginDTO) {

        System.out.println("用户登录");

        User user = userMapper.getUserByNameAndPwd(loginDTO.getName(), loginDTO.getPwd());
        if (user == null) {
            return Result.fail("用户名或密码错误");
        }

        String uuid = "user-token:" + UUID.randomUUID();

        stringRedisTemplate.opsForValue().set(uuid, user.getId() + "",30, TimeUnit.MINUTES);

        LoginVO loginVO = new LoginVO(uuid, user.getType(), user.getName(), user.getId());

        return Result.ok(loginVO);
    }

    @PostMapping("/register")
    public Result register(@RequestBody RegisterDTO registerDTO) {

        if (StringUtil.isNullOrEmpty(registerDTO.getName())|| StringUtil.isNullOrEmpty(registerDTO.getPwd())){
            return Result.fail("用户名或密码不能为空");
        }

        User user = userMapper.getUserByTelephone(registerDTO.getTelephone());
        if (user != null) {
            return Result.fail("该手机号已注册账号");
        }

        Integer userId = userMapper.getMaxId() + 1;
        System.out.println("用户注册");
        userMapper.insert(new User(userId,registerDTO.getName(), registerDTO.getPwd(), registerDTO.getTelephone(), LocalDateTime.now()));

        return Result.ok();
    }

    @PostMapping("/logout")
    public Result logout(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        System.out.println("token:" + token);
        if (StringUtil.isNullOrEmpty(token)) {
            return Result.fail("用户未登录");
        }
        System.out.println("用户退出登录");
        stringRedisTemplate.delete(token);
        return Result.ok();
    }

    @GetMapping("/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {
        System.out.println("获取用户信息");

        String token = request.getHeader("Authorization");

        System.out.println("token:" + token);
        if (StringUtil.isNullOrEmpty(token)){
            return Result.fail("未获取到token信息");
        }
        Integer userId = Integer.valueOf(stringRedisTemplate.opsForValue().get(token));
        if (userId == null){
            return Result.fail("用户未登录");
        }
        User user = userMapper.getById(userId);
        UserVO userVO = new UserVO(user.getId(),user.getName(), user.getPwd(), user.getTelephone(), user.getType() == 1 ? "普通用户" : "管理员", user.getDate());
        return Result.ok(userVO);
    }

    @PostMapping("/updateUserInfo")
    public Result updateUserInfo(@RequestBody UserDTO userDTO, HttpServletRequest request) {

        System.out.println("更新用户信息");
        String token = request.getHeader("Authorization");


        if (StringUtil.isNullOrEmpty(token)){
            return Result.fail("未获取到token信息");
        }

        Integer userId = Integer.valueOf(stringRedisTemplate.opsForValue().get(token));

        if (userId == null){
            return Result.fail("用户未登录");
        }

        User user = userMapper.getById(userId);

        if (user == null){
            return Result.fail("用户不存在");
        }

        user.setName(userDTO.getName());
        user.setPwd(userDTO.getPwd());
        userMapper.updateById(user);
        return Result.ok();
    }

}
